// pages/order-detail/order-detail.js
var util = require("../../utils/util.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getOrderDetail(options.id)
  },
  sendMsg: function () {
    this.setData({
      isShowFreight: !0
    });
  },
  getOrderDetail: function (val) {
    const _this = this
    util.request(getApp().api.goods.orderDetail,{
      orderId: val,
    }).then(
      function(res) {
        if (res.code == 200) {
          res.data2.forEach(item =>{
            var month = new Date(item.pickupTime).getMonth();
            var day = new Date(item.pickupTime).getDate();
            item.pickupTime = month+'月'+day+'日';
          })
          _this.setData({
            order_detail: res.data,
            address_detail: res.data2
          });
          if (res.data.orderStatus == 0) {
            var order_time = new Date(res.data.createtime).getTime();
            // var test = '2020-06-01 12:45:14'
            // var order_time = new Date(test).getTime();
						var cancelOrderTime = order_time + 86400000;
						_this.setData({
							timer: setInterval(function() {
								var remainTime = cancelOrderTime - new Date().getTime();
								if (remainTime > 0) {
									_this.setData({
										isshow_time: !0,
										cancel_order_time: _this.getRemainTime(remainTime)
									})
								} else {
									clearInterval(_this.data.timer);
									// _this.cancelOrder();
								}
							}, 1000)
            })
					} else if (res.data.orderStatus == 2) {
            var order_time = new Date(res.data.createtime).getTime();
            // var test = '2020-06-01 12:45:14'
            // var order_time = new Date(test).getTime();
            var confirmOrderTime = order_time + 604800000;
            _this.setData({
              secondTimer: setInterval(function() {
                var leftTime = confirmOrderTime - new Date().getTime();
                if (leftTime > 0) {
                  _this.setData({
                    isshow_time: !0,
                    confirm_order_time: _this.getRemainTime(leftTime)
                  })
                } else {
                  clearInterval(_this.data.secondTimer);
                  // _this.confirm();
                }
              }, 1000)
            })
          }
        } else {
          wx.showToast({
            title: res.msg, //提示的内容,
            icon: "none", //图标,
            //duration: 1500, //延迟时间,
            mask: false, //显示透明蒙层，防止触摸穿透,
          });
          setTimeout(function() {
            wx.hideToast();
          }, 1500);
          return
        }
      }
    );
  },
  getRemainTime: function(e) {
		if(e > 86400000){
			var d = parseInt(e / 1000 / 60 / 60 / 24, 10);
		}
		var h = parseInt(e / 1000 / 60 / 60 % 24, 10);
		var m = parseInt(e / 1000 / 60 % 60, 10);
		var s = parseInt(e / 1000 % 60, 10);
		if (h < 10) {
			h = '0' + h
		}
		if (m < 10) {
			m = '0' + m
		}
		if (s < 10) {
			s = '0' + s
		}
		if(e > 86400000){
			return d +'天'+ h + ':' + m + ':' + s;
		}else{
			return h + ':' + m + ':' + s;
		}
  },
  toAfterSale: function (e) {
    wx.navigateTo({
        url: '/pages/after-sale/after-sale?id=' + e.currentTarget.dataset.id
    })
  },
  cancelOrder: function(e) {
    util.request(getApp().api.goods.cancelOrder,{
      orderId: e.currentTarget.dataset.id
    }).then(
      function(res) {
        if (res.code == 200) {
          wx.navigateTo({
              url: '/pages/order-list/order-list?id=0'
          })
        } else {
          wx.showToast({
            title: res.msg, //提示的内容,
            icon: "none", //图标,
            //duration: 1500, //延迟时间,
            mask: false, //显示透明蒙层，防止触摸穿透,
          });
          setTimeout(function() {
            wx.hideToast();
          }, 1500);
          return
        }
      }
    );
  },
  toPay: function(e) {
    const _this = this
    util.request(getApp().api.wxpay.pay,{
      orderId: e.currentTarget.dataset.id
    }).then(
      function(res) {
        if (res.code == 200) {
          wx.navigateTo({
              url: '/pages/order-list/order-list?id=0'
          })
        } else {
          wx.showToast({
            title: res.msg, //提示的内容,
            icon: "none", //图标,
            //duration: 1500, //延迟时间,
            mask: false, //显示透明蒙层，防止触摸穿透,
          });
          setTimeout(function() {
            wx.hideToast();
          }, 1500);
          return
        }
      }
    );
  },
  confirm: function (e) {
    util.request(getApp().api.goods.confrim,{
      subOrderId: e.currentTarget.dataset.id
    }).then(
      function(res) {
        if (res.code == 200) {
          wx.navigateTo({
              url: '/pages/order-list/order-list?id=0'
          })
        } else {
          wx.showToast({
            title: res.msg, //提示的内容,
            icon: "none", //图标,
            //duration: 1500, //延迟时间,
            mask: false, //显示透明蒙层，防止触摸穿透,
          });
          setTimeout(function() {
            wx.hideToast();
          }, 1500);
          return
        }
      }
    );
  },
  searchExpress: function () {
    wx.navigateTo({
        url: '/pages/search-express/search-express'
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})